package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DeptDescDTO;
import com.pshcp.domain.model.DeptDesc;
import com.pshcp.mapper.DeptDescMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 科室介绍 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2023-03-22
 */
@Service
public class DeptDescService {

    @Autowired
    private DeptDescMapper deptDescMapper;

    public PageList<DeptDesc> list(DeptDescDTO deptDescDTO){
        if(deptDescDTO.getPageNum() != null && deptDescDTO.getPageSize() != null){
            PageHelper.startPage(deptDescDTO.getPageNum(), deptDescDTO.getPageSize());
        }
        Example example = new Example(DeptDesc.class);
        Example.Criteria criteria = example.createCriteria();
        if(deptDescDTO.getStatus() != null){
            criteria.andEqualTo("status", deptDescDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(!ObjectUtils.isEmpty(deptDescDTO.getHosArea())){
            criteria.andEqualTo("hosArea", deptDescDTO.getHosArea());
        }
        if(!ObjectUtils.isEmpty(deptDescDTO.getDeptName())){
            criteria.andLike("deptName", "%"+deptDescDTO.getDeptName()+"%");
        }
        example.setOrderByClause(" sort");
        List<DeptDesc> list = deptDescMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DeptDesc> select(DeptDesc deptDesc){
        return deptDescMapper.select(deptDesc);
    }

    public int insert(DeptDesc deptDesc){
        deptDesc.setId(SystemUtil.getId());
        deptDesc.setStatus(CommonStatusEnum.INIT.getCode());
        deptDesc.setCreatedTime(new Date());
        return deptDescMapper.insert(deptDesc);
    }

    public int update(DeptDesc deptDesc){
        deptDesc.setUpdateTime(new Date());
        return deptDescMapper.updateByPrimaryKeySelective(deptDesc);
    }

    public DeptDesc findById(Long id){
        return deptDescMapper.selectByPrimaryKey(id);
    }

    public List<DeptDesc> findByIds(List<Long> ids){
        Example example = new Example(DeptDesc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return deptDescMapper.selectByExample(example);
    }

    public List<DeptDesc> findByDeptName(String deptName){
        Example example = new Example(DeptDesc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deptName", deptName);
        return deptDescMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DeptDesc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DeptDesc deptDesc = new DeptDesc();
        deptDesc.setStatus(CommonStatusEnum.DEL.getCode());
        return deptDescMapper.updateByExampleSelective(deptDesc, example);
    }

}
